package com.operate.api.app.info.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import com.operate.api.app.info.dao.CompanyDao;
import com.operate.api.app.info.domain.po.CompanyPO;
import com.operate.api.app.info.domain.vo.*;
import com.operate.api.app.info.service.IAppCompanyService;
import com.operate.api.app.user.dao.AppUserDao;
import com.operate.api.app.user.domain.po.AppUserPO;
import com.operate.api.app.user.domain.vo.*;
import com.operate.api.base.*;
import com.operate.api.operate.user.domain.po.OperateUserPO;
import com.operate.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
@Transactional
@Slf4j
public class AppCompanyServiceImpl implements IAppCompanyService {

    @Autowired
    private CompanyDao dao;


    @Override
    public List<AppCompanyVO> getInfoList(AppCompanySearchVO searchVO) {
        return dao.getList(searchVO);
    }

    @Override
    public AppCompanyVO getCompanyOne(IDEntity id) {
        return dao.getOne(id.getId());
    }


    @Override
    public List<RecommendCompanyVO> getCompanyList(RecommendCompanySearchVO searchVO) {
        return dao.getCompanyList(searchVO);
    }

    @Override
    public void updateRecommend(RecommendCompanyUpdateVO updateVO) {
        CompanyPO companyPO= dao.selectByPrimaryKey(updateVO.getId());
        if(companyPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "推荐信息不存在");
        }
        BeanUtils.copyProperties(updateVO, companyPO);
        companyPO.preUpdate();
        int i = dao.updateByPrimaryKeySelective(companyPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void batchRecommend(IDsEntity iDsEntity) {
        for (String id : iDsEntity.getIds()){
            CompanyPO companyPO= dao.selectByPrimaryKey(id);
            companyPO.setIsRecommend(1);
            companyPO.preUpdate();
            companyPO.setRecommendDate(companyPO.getUpdateDate());
            int i = dao.updateByPrimaryKeySelective(companyPO);
            if (i == Constants.SQL_RETURN_SUCCESS) {
                throw new BusinessException(Type.FAIL);
            }
        }
    }
}